package com.wawu.server.dao;

import com.wawu.common.annotation.IOC.component.Repository;
import com.wawu.common.utils.DruidUtil;
import com.wawu.common.utils.SQLExcutorUtil;
import com.wawu.pojo.entity.GoodType;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;


import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

@Repository
public class GoodTypesDAO {
    public List<GoodType> getGoodTypes() throws SQLException {
        String sql="SELECT * FROM types";
        return SQLExcutorUtil.queryList(sql,GoodType.class);
    }



    /**
     * 获取所有商品类型
     * @return
     */
    public List<GoodType> testGetGoodType(){
        QueryRunner queryRunner = new QueryRunner();
        String sql = "SELECT * FROM types"; // 查询所有用户
        try (Connection connection = DruidUtil.getConnection()) {
            return queryRunner.query(connection, sql, new BeanListHandler<>(GoodType.class));
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    //测试下各种方法
    public static void main(String[] args) throws SQLException {
        GoodTypesDAO goodTypesDAO =new GoodTypesDAO();
        System.out.println(goodTypesDAO.testGetGoodType());

        String sql = "SELECT * FROM types"; // 查询所有用户
        List<GoodType> goodTypes = SQLExcutorUtil.queryList(sql, GoodType.class);
        System.out.println(goodTypes);

        sql = "SELECT * FROM types where name like concat('%',?,'%')"; // 查询所有用户
        List<GoodType> goodType = SQLExcutorUtil.queryList(sql, GoodType.class,"o");
        System.out.println(goodType);
    }

    public int insert(String typeName) throws SQLException {
        String sql="INSERT INTO types(name) VALUES(?)";
        return SQLExcutorUtil.update(sql,typeName);
    }
}
